package com.qiche.controller.mgr;

import com.github.pagehelper.PageInfo;
import com.qiche.aspect.Result;
import com.qiche.dto.ResourceDto;
import com.qiche.dto.ResourceParentDto;
import com.qiche.model.Resources;
import com.qiche.model.Role;
import com.qiche.model.User;
import com.qiche.service.ResourcesService;
import com.qiche.service.RoleService;
import com.qiche.shiro.ShiroService;
import com.qiche.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资源管理控制器
 * Created by Linzw on 2018/01/24.
 */
@Api(value = "/cards",description = "资源管理控制器")
@RestController
@RequestMapping("/resources")
public class ResourcesController {

	@Resource
	private ResourcesService resourcesService;
	@Resource
	private RoleService roleService;
	@Resource
	private ShiroService shiroService;

	@ApiOperation(value ="根据条件和分页获取资源列表" )
	@RequestMapping
	public Map<String, Object> getAll(Resources resources, @RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int limit) {
		Map<String, Object> map = new HashMap<>();
		PageInfo<Resources> pageInfo = resourcesService.selectByPage(resources, page, limit);
		map.put("code", 0);
		map.put("msg", "查询成功");
		map.put("count", pageInfo.getTotal());
		map.put("data", pageInfo.getList());
		return map;
	}


	@ApiOperation(value ="根据角色ID获取带勾选情况的资源列表" )
	@RequestMapping("/resourcesWithSelected")
	public Result resourcesWithSelected(Integer rid) {
		User currentUser=UserUtils.getCurrentUser();
		Role role=roleService.queryRoleByUserId(currentUser.getId());
		List<Resources> resources =null;
		if(UserUtils.checkIsAdmin(currentUser)){
			resources=resourcesService.queryTopResourcesListWithSelected(rid);
		}else{
			resources=resourcesService.queryResourcesListWithSelected(rid,role.getId());
		}
		return Result.createBySuccess(resources);
	}

	@ApiOperation(value ="PC端左侧导航栏" )
	@RequestMapping("/loadMenu")
	public List<ResourceParentDto> loadMenu() {
		Map<String, Object> map = new HashMap<>();
		Integer userid = (Integer) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
		map.put("type", 1);
		map.put("userid", userid);
		List<ResourceParentDto> parentDtos=new ArrayList<>();
		List<Resources> Resources=resourcesService.selectByParentId(0);
		Map<Integer,List<ResourceDto>> composeMap=new HashMap<>();
		for (Resources res: Resources) {
			composeMap.put(res.getId(),new ArrayList<>());

		}
		List<Resources> resourcesList = resourcesService.loadUserResources(map);
		List<ResourceDto> list = new ArrayList<>();
		for (Resources resources : resourcesList) {
			ResourceDto dto = new ResourceDto();
			dto.setTitle(resources.getName());
			dto.setHref(resources.getResurl());
			dto.setSpread(false);
			dto.setIcon(resources.getIcon());
			composeMap.get(resources.getParentid()).add(dto);
		}
		for (Map.Entry<Integer,List<ResourceDto>> entry:composeMap.entrySet()
			 ) {
			//屏蔽无子级菜单权限的父级菜单
			if(entry.getValue().size()==0){
				continue;
			}
			Resources resources=resourcesService.selectByKey(entry.getKey());
			ResourceParentDto parentDto=new ResourceParentDto();
			parentDto.setTitle(resources.getName());
			parentDto.setIcon(resources.getIcon());
			parentDto.setSpread(true);
			parentDto.setChildren(entry.getValue());
			parentDtos.add(parentDto);
		}
		return parentDtos;
	}

	@ApiOperation(value ="添加资源" )
	@RequestMapping(value = "/add")
	public Result add(Resources resources) {
		int insertCount=resourcesService.save(resources);
		if(insertCount>0){
			// 更新权限
			shiroService.updatePermission();
			return Result.createBySuccessMessage("添加成功");
		}
		return  Result.createByErrorMessage("添加失败");
	}

	@ApiOperation(value ="删除资源" )
	@RequestMapping(value = "/delete")
	public Result delete(Integer id) {
		int deleteCoount=resourcesService.delete(id);
		if(deleteCoount>0){
			// 更新权限
			shiroService.updatePermission();
			return Result.createBySuccessMessage("删除成功");
		}
		return  Result.createByErrorMessage("删除失败");
	}
}
